package com.fakeDYChat.common.common.config;

import com.fakeDYChat.common.common.thread.MyThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

import static com.fakeDYChat.common.common.thread.ThreadPoolNameConstant.WS_EXECUTOR;

@Configuration
public class WSThreadPoolConfig {

    @Bean(WS_EXECUTOR)
    @Primary
    public ThreadPoolTaskExecutor fakeDYExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置线程池优雅停机
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //设置线程池销毁时，优雅停机的等待时间
        executor.setAwaitTerminationSeconds(10);
        executor.setCorePoolSize(16);
        executor.setMaxPoolSize(16);
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("fakeDY-ws-executor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());//满了调用线程执行，认为重要任务
        executor.setThreadFactory(new MyThreadFactory(executor));
        executor.initialize();
        return executor;
    }
}